<template>
  <!-- <el-dialog :title="title" :visible.sync="openDialog" @close="close"> -->
  <el-drawer :title="title" :visible.sync="openDialog" size="50%" @closed="handleClose">
    <el-row type="flex" justify="center">
      <el-col :span="18">
        <el-form ref="addform" :model="form" label-suffix="：" :rules="rules" label-position="right" label-width="120px">
          <el-form-item label="品牌名称" prop="name">
            <el-input v-model="form.name" autocomplete="off" />
          </el-form-item>
          <el-form-item label="品牌首字母" prop="first_name">
            <el-input v-model="form.first_name" autocomplete="off" />
          </el-form-item>
          <el-form-item label="品牌LOGO" prop="img">
            <!-- <pt-upload v-model="form.img" :width="314" :height="168" ></pt-upload> -->
            <image-upload ref="upimg" v-model="form.img" :width="314" :height="168" :showWidth="157" :showHeight="84" />
          </el-form-item>
          <el-form-item label="供应商" prop="shop_id">
            <el-select v-model="form.shop_id" placeholder="请选择">
              <el-option v-for="item in gysList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <div class="drawer-footer">
      <el-button @click="openDialog = false">取 消</el-button>
      <el-button type="primary" @click="save" :disabled="loading" :loading="loading">{{ loading ? '提交中 ...' : '确 定' }}</el-button>
    </div>
  </el-drawer>
</template>
<script>
import { mapState } from 'vuex'
export default {
  data() {
    return {
      id: '',
      title: '新增品牌信息',
      openDialog: false,
      loading:false,
      form: {
        name: '',
        first_name: '',
        shop_id: '',
        img: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入品牌名称', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        first_name: [{ pattern: /^[A-Za-z]{1}$/, message: '只能输入A-Z一位字母', trigger: 'change' }],
        img: [{ required: true, message: '必须上传品牌Logo', trigger: 'change' }],
        shop_id: [{ required: true, message: '请选择品牌供应商', trigger: 'change' }]
      }
    }
  },
  computed: {
    ...mapState(['gysList']),
    queryData() {
      let obj = this.form
      obj.id = this.id
      return obj
    }
  },
  methods: {
    init(val) {
      this.openDialog = true
      if (val) {
        this.title = '修改品牌信息'
        this.id = val.id
        this.form = val
      } else {
        this.title = '新增品牌信息'
        this.id = ''
      }
    },
    handleClose(){
      // this.$refs.addform.resetFields()
      this.form = this.$options.data().form
      this.openDialog = false
    },
    save() {
      this.$refs.addform.validate(valid => {
        if (valid) {
          // this.loading = true
          this.$http.httpPost('/brand/save', this.queryData).then(res => {
            // this.loading = false
            if (res.status === 200) {
              this.$emit('save')
              this.openDialog = false
              this.$operateTip()
            }else{
              this.$message.error('请求失败！')
            }
          })
        }
      })
    }
  }
}
</script>
<style lang="less" scoped></style>
